$(function () {
    //省
    $.get(baseUrl + '/system/province', { parentId: 1 }, function (res) {
        console.log(res.data);
        html = '';
        res.data.forEach(function (itme) {
            html += `<option value="${itme.id}">${itme.name}</option>`;
        });
        $('.sheng').html(html);
    });
    //市
    $('.sheng').on('change', function () {
        let id = $(this).val();
        $('.shi').html('<option value="">请选择</option>')
        $('.qu').html('<option value="">请选择</option>')
        //console.log(id);
        $.get(baseUrl + '/system/province', { parentId: id }, function (res) {
            html = '';
            res.data.forEach(function (itme) {
                html += `<option value="${itme.id}">${itme.name}</option>`
            })
            $('.shi').html(html);
        })
    });
    //区
    $('.shi').on('change', function () {
        let id = $(this).val();
        $.get(baseUrl + '/system/province', { parentId: id }, function (res) {
            html = '';
            res.data.forEach(function (itme) {
                html += `<option value="${itme.id}">${itme.name}</option>`
            });
            $('.qu').html(html)
        })

    })
    //设置隐藏域
    console.log(userInfo.id);
    $('.isDefaultChange').on('click', function () {
        if ($(this).prop('checked')) {
            $('.isDefault').val(1);
        } else {
            $('.isDefault').val(0);
        }
    });
    //添加地址库
    $('.btn').on('click', function () {
        let data = $('.add_address').serialize();
       // console.log(data);
        data += '&userId=' + userInfo.id;
        //html = '';
        $.post(baseUrl + '/address/add', data, function (res) {
           // console.log(res);
            // let consignee = $('.consignee').val();
            // let sheng = $('.sheng option:selected').text();
            // let shi = $('.shi option:selected').text();
            // let qu = $('.qu option:selected').text();
            // let address = $('.address').val();
            // let mobile = $('.mobile').val();
            // html += `<dt>2,${consignee} ${sheng}  ${shi}  ${qu}  ${address}  ${mobile}</dt>
            // <dd>
			// 			<a href="javascript:;">修改</a>
			// 			<a href="javascript:;">删除</a>
			// 			<a href="javascript:;">设为默认地址</a>
			// 		</dd> `;
            // $('.last').html(html)
            // console.log();
        })
        location.reload();
        return false;
    });
    //地址库列表
    $.get(baseUrl+'/address/lists',{userId:userInfo.id},function(res){
        console.log(res.data);
        html = '<h3>收货地址薄</h3>';
        res.data.forEach(function(itme,index){
           // console.log(itme);
            html+=`<dl data-id="${itme.id}" consignee="${itme.consignee}" 
            mobile="${itme.mobile}" province="${itme.province}" city="${itme.city}"
            area =${itme.area} address="${itme.address}" isDefault="1"
             userId="${userInfo.id}">
            <dt>${index-0+1}.${itme.consignee} ${itme.provinceName} ${itme.cityName}${itme.areaName} ${itme.address} ${itme.mobile} </dt>
            <dd>
                <a href="javascript:;" class="changeAdd">修改</a>
                <a href="javascript:;" class="deleteAdd">删除</a>
                <a href="javascript:;" class="moren">设为默认地址</a>
            </dd>
        </dl>`
        })
        $('.address_hd').html(html)
    })
    //删除地址
    $('.address_hd').on('click','.deleteAdd',function(){
      let addressId =  $(this).parents('dl').attr('data-id');
        $.get(baseUrl+'/address/remove',{addressId:addressId,userId:userInfo.id},function(res){
            console.log(res);
        })
        $(this).parents('dl').remove();
        location.reload();
    })
    //修改地址
    $('.address_hd').on('click','.changeAdd',function(){
        let addressId = $(this).parents('dl').attr('data-id');
        let userId = $(this).parents('dl').attr('userId');
        $.get(baseUrl+'/address/detail',{addressId,userId},function(res){
            console.log(res.data);
            $('.consignee').val(res.data.consignee);
            $('.address').val(res.data.address);
            $('.mobile').val(res.data.mobile);

        })
        
        // let consignee = $(this).parents('dl').attr('consignee');
        // let mobile = $(this).parents('dl').attr('mobile');
        // let province = $(this).parents('dl').attr('province');
        // let city = $(this).parents('dl').attr('city');
        // let area = $(this).parents('dl').attr('area');
        // let address = $(this).parents('dl').attr('address');
        // let isDefault = $(this).parents('dl').attr('isDefault');
       
        // console.log(addressId,consignee,mobile,province,city
        //     ,area,address,isDefault,userId);

        // $.post(baseUrl+'/address/change',{addressId,consignee,mobile,province,city
        // ,area,address,isDefault,userId},function(res){
        //     console.log(res);
        // })
    })
//保存修改后的地址
$('.xiugai').click(function(){
  let addressId =  $(this).parents('.content').find('.address_hd>dl').attr('data-id');
  console.log(addressId);
  let data ='addressId='+addressId ;
    data += '&'+$('.add_address').serialize();
    // console.log(data);
     data += '&userId=' + userInfo.id;
     
console.log(data);
    $.post(baseUrl+'/address/change',data,function(res){
        console.log(res);
        location.reload();
    })
    return false;
})








})